﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

public class Solution283

{
    //先暴力一下
    public void MoveZeroes2(int[] nums)
    {
        int len=nums.Length;
        int i = 0;

        while (i < len )
        {
            if (nums[i] == 0)
            {
                int cur = i;

                while (cur < len-1)
                {


                    Swap(nums, cur, cur + 1);
                    cur++;
                }
                len--;

            }
            else
               
            {
                i++;
            }
           

        }




    }
    //记录前面有几个0，然后把数字向前移动
    public void MoveZeroes3(int[] nums)
    {

        int num0 = 0;
    for (int i = 0; i < nums.Length; i++)
        {


            if (nums[i] == 0) {
                num0++;
            }
            else//向前移动
            {

                nums[i-num0] = nums[i];

            }

        }
    //最后几个0

    for(int i = nums.Length-1; i >= nums.Length - num0; i--)
        {

            nums[i] = 0;
        }
    
    
    
    }
    //双指针
    public void MoveZeroes(int[] nums)
    {

        int len = nums.Length;

        int left=0, right=0;

        while (right < nums.Length)
        {


            if (nums[right] != 0)
            {
                Swap(nums, left, right);
                left++;
                
            }

            right++;
        }

    }



        public void Swap(int[] nums,int i,int j)
    {
        int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp;

    }
}

class Program283
{
    static void Main283(string[] args)
    {
        Solution283 solution = new Solution283();
        int[] nums = [0, 1, 0, 3, 12];
        solution.MoveZeroes(nums);

        for (int i = 0; i < nums.Length; i++)
        {
            Console.Write(nums[i]+",");
        }
    }
}
